package com.cruise.game.service.impl.common;

import com.cruise.game.common.constants.CacheConstants;
import com.cruise.game.common.utils.RedisUtils;
import com.cruise.game.mapper.AnnouncementMapper;
import com.cruise.game.mapper.CarouselMapper;
import com.cruise.game.mapper.GameCategoryMapper;
import com.cruise.game.mapper.GameServiceMapper;
import com.cruise.game.model.entity.Announcement;
import com.cruise.game.model.entity.Carousel;
import com.cruise.game.model.entity.GameCategory;
import com.cruise.game.model.vo.common.HomeVO;
import com.cruise.game.service.common.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页服务实现
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private AnnouncementMapper announcementMapper;

    @Resource
    private CarouselMapper carouselMapper;

    @Resource
    private GameCategoryMapper gameCategoryMapper;

    @Resource
    private GameServiceMapper gameServiceMapper;

    @Override
    public HomeVO getHomeData() {
        // 从缓存中获取首页数据
        HomeVO homeVO = (HomeVO) redisUtils.get(CacheConstants.HOME_DATA_CACHE);
        if (homeVO != null) {
            return homeVO;
        }
        
        // 缓存中没有，从数据库中获取
        homeVO = new HomeVO();
        
        // 获取通告列表
        List<Announcement> announcements = announcementMapper.selectActiveList();
        List<HomeVO.AnnouncementVO> announcementVOs = new ArrayList<>();
        for (Announcement announcement : announcements) {
            HomeVO.AnnouncementVO vo = new HomeVO.AnnouncementVO();
            vo.setId(announcement.getId());
            vo.setTitle(announcement.getTitle());
            vo.setContent(announcement.getContent());
            announcementVOs.add(vo);
        }
        homeVO.setAnnouncements(announcementVOs);
        
        // 获取轮播图列表
        List<Carousel> carousels = carouselMapper.selectActiveList();
        List<HomeVO.CarouselVO> carouselVOs = new ArrayList<>();
        for (Carousel carousel : carousels) {
            HomeVO.CarouselVO vo = new HomeVO.CarouselVO();
            vo.setId(carousel.getId());
            vo.setImageUrl(carousel.getImageUrl());
            vo.setLinkUrl(carousel.getLinkUrl());
            vo.setTitle(carousel.getTitle());
            carouselVOs.add(vo);
        }
        homeVO.setCarousels(carouselVOs);
        
        // 获取游戏分类列表
        List<GameCategory> categories = gameCategoryMapper.selectActiveList();
        List<HomeVO.GameCategoryVO> categoryVOs = categories.stream()
                .map(category -> {
                    HomeVO.GameCategoryVO vo = new HomeVO.GameCategoryVO();
                    vo.setId(category.getId());
                    vo.setName(category.getName());
                    vo.setIconUrl(category.getIconUrl());
                    return vo;
                })
                .collect(Collectors.toList());
        homeVO.setCategories(categoryVOs);
        
        // 获取热门服务列表
        // TODO: 实现热门服务列表
        homeVO.setHotServices(new ArrayList<>());
        
        // 获取推荐服务列表
        // TODO: 实现推荐服务列表
        homeVO.setRecommendedServices(new ArrayList<>());
        
        // 获取最近订单列表
        // TODO: 实现最近订单列表
        homeVO.setRecentOrders(new ArrayList<>());
        
        // 缓存首页数据
        redisUtils.set(CacheConstants.HOME_DATA_CACHE, homeVO, CacheConstants.HOME_DATA_CACHE_EXPIRE);
        
        return homeVO;
    }
}
